﻿using System;
using System.Configuration;
using System.Windows;
using AccountManagement.DataAccess;
using AccountManagement.Reports;
using CrystalDecisions.CrystalReports.Engine;

namespace AccountManagement
{
    /// <summary>
    /// Interaction logic for FrmReport.xaml
    /// </summary>
    public partial class FrmReport
    {
        private readonly tblPhieuThuChi _objPhieuThuChi;
        public FrmReport()
        {
            InitializeComponent();
        }

        public FrmReport(tblPhieuThuChi objPhieu)
        {
            InitializeComponent();
            _objPhieuThuChi = objPhieu;
        }

        private void DisplayReport()
        {
            try
            {
                if (_objPhieuThuChi == null) return;

                ReportClass report;
                if (_objPhieuThuChi.IsIncoming)
                    report = new PhieuThu();
                else
                {
                    report = new PhieuChi();
                }

                report.DataDefinition.FormulaFields["strDonVi"].Text = ToString(ConfigurationManager.AppSettings["TenCongTy"]);
                report.DataDefinition.FormulaFields["strDiaChiCty"].Text = ToString(ConfigurationManager.AppSettings["DiaChi"]);
                report.DataDefinition.FormulaFields["strQuyenSo"].Text = ToString(_objPhieuThuChi.QuyenSo.ToString());
                report.DataDefinition.FormulaFields["strPhieuSo"].Text = ToString(_objPhieuThuChi.PhieuSo.ToString());
                report.DataDefinition.FormulaFields["strNgayThang"].Text = ToString("Ngày " + _objPhieuThuChi.NgayThang.Day + " tháng " + _objPhieuThuChi.NgayThang.Month + " năm " + _objPhieuThuChi.NgayThang.Year);
                report.DataDefinition.FormulaFields["strTKNo"].Text = ToString(_objPhieuThuChi.TKNo.ToString());
                report.DataDefinition.FormulaFields["strTKCo"].Text = ToString(_objPhieuThuChi.TKCo.ToString());
                report.DataDefinition.FormulaFields["strHoTen"].Text = ToString(_objPhieuThuChi.HoTen);
                report.DataDefinition.FormulaFields["strDiaChi"].Text = ToString(_objPhieuThuChi.DiaChi);
                report.DataDefinition.FormulaFields["strLyDoThu"].Text = ToString(_objPhieuThuChi.LyDo);
                report.DataDefinition.FormulaFields["strSoTien"].Text = ToString(_objPhieuThuChi.Sotien.ToString("0,0") + " đ");
                report.DataDefinition.FormulaFields["strVietBangChu"].Text = ToString(_objPhieuThuChi.VietBangChu);
                report.DataDefinition.FormulaFields["strKemTheo"].Text = ToString(_objPhieuThuChi.KemTheo);
                report.DataDefinition.FormulaFields["strConfirm"].Text = ToString(_objPhieuThuChi.DaNhanDu);

                crystalReportViewer.ReportSource = report;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DisplayReport();
        }

        private static string ToString(string input)
        {
            return "\"" + input + "\"";
        }
    }
}
